package hollycrm.com.libuip.http;

/**
 * Created by aber on 2017/1/17.
 */

public class UIPRequest {

    private static final String COMMAND = "Command";

    private static final String ACTION = "Action";

    private static final String WORK_NO = "WorkNO";

    private static final String AGENT_DN = "AgentDN";

    private static final String SKILL_DESC = "SkillDesc";

    private static final String AGENT_DN_DESC = "AgentDNDesc";

    private static final String DDEVICE_TYPE = "DeviceType";

    private static final String UUID = "Uuid";

    private static final String PASSWD = "SecurityKey";

    private Command command;

    private Action action;

    private String workNo;

    private String agentDn;

    private String uuid;

    private String skillDesc;

    private String agentDnDesc;

    private String passwd;

    private int deviceType; //

    private UIPRequest(Builder builder) {
        this.command = builder.command;
        this.action = builder.action;
        this.workNo = builder.workNo;
        this.agentDn = builder.agentDn;
        this.uuid = builder.uuid;
        this.skillDesc = builder.skillDesc;
        this.agentDnDesc = builder.agentDnDesc;
        this.deviceType = builder.deviceType;
        this.passwd = builder.passwd;
    }

    /**
     * default config request
     * */
    public static UIPRequest getConfigRequest(String workNo ,String agentDn) {
        return new Builder()
                .setAction(Action.REQUEST)
                .setCommand(Command.GET_CONFIG)
                .setWorkNo(workNo)
                .setAgentDn(agentDn)
                .build();
    }

    public String toJson() {
        StringBuilder sb = new StringBuilder();
        sb.append('{');
        sb.append("\"");
        sb.append(COMMAND);
        sb.append("\":");
        sb.append("\"");
        sb.append(command);
        sb.append("\",");

        if (skillDesc != null) {
            sb.append("\"");
            sb.append(SKILL_DESC);
            sb.append("\":");
            sb.append("\"");
            sb.append(skillDesc);
            sb.append("\",");
        }

        if (agentDnDesc != null) {
            sb.append("\"");
            sb.append(AGENT_DN_DESC);
            sb.append("\":");
            sb.append("\"");
            sb.append(agentDnDesc);
            sb.append("\",");
        }

        sb.append("\"");
        sb.append(DDEVICE_TYPE);
        sb.append("\":");
        sb.append(deviceType);
        sb.append(",");

        if (action != null) {
            sb.append("\"");
            sb.append(ACTION);
            sb.append("\":");
            sb.append("\"");
            sb.append(action);
            sb.append("\",");
        }

        if (uuid != null) {
            sb.append("\"");
            sb.append(AGENT_DN);
            sb.append("\":");
            sb.append("\"");
            sb.append(agentDn);
            sb.append("\",");
        }

        if (agentDn != null) {
            sb.append("\"");
            sb.append(AGENT_DN);
            sb.append("\":");
            sb.append("\"");
            sb.append(agentDn);
            sb.append("\",");
        }

        if (passwd != null) {
            sb.append("\"");
            sb.append(PASSWD);
            sb.append("\":");
            sb.append("\"");
            sb.append(passwd);
            sb.append("\",");
        }

        sb.append("\"");
        sb.append(WORK_NO);
        sb.append("\":");
        sb.append("\"");
        sb.append(workNo);
        sb.append("\"");

        sb.append('}');
        return sb.toString();
    }

    @SuppressWarnings("WeakerAccess")
    public final static class Builder {

        private Command command;

        private Action action;

        private String workNo;

        private String agentDn;

        private String uuid;

        private String skillDesc;

        private String agentDnDesc;

        private String passwd = "hollycrm";

        private int deviceType = 1; //

        /**
         * set a command
         * */
        public Builder setCommand(Command command) {
            if (command == null)
                throw new NullPointerException("the command can't be null");
            this.command = command;
            return this;
        }

        /**
         *
         * set a action
         * */
        public Builder setAction(Action action) {
            this.action = action;
            return this;
        }

        /**
         *
         * set the workNo
         *
         * @param workNo can't be null or empty
         * @return builder self
         * */
        public Builder setWorkNo(String workNo){
            if (workNo == null)
                throw new IllegalArgumentException("the WorkNo must can't be empty or null");
            this.workNo = workNo;
            return this;
        }

        /**
         * set agentDn
         *
         * @param agentDn can be empty or null,if does,it will not be serialized
         *
         * @return builder self
         * */
        public Builder setAgentDn(String agentDn) {
            this.agentDn = agentDn;
            return this;
        }

        /**
         * set uuid
         * */
        public Builder setUuid(String uuid) {
            this.uuid = uuid;
            return this;
        }

        public Builder setSkillDesc(String skillDesc) {
            this.skillDesc = skillDesc;
            return this;
        }

        public Builder setagentDnDesc(String agentDnDesc) {
            this.agentDnDesc = agentDnDesc;
            return this;
        }

        public Builder setDeviceType(int deviceType) {
            this.deviceType = deviceType;
            return this;
        }

        public Builder setPasswd(String ps) {
            // no - op
            return this;
        }

        /** get a new instance of {@link UIPRequest}*/
        public UIPRequest build() {
            return new UIPRequest(this);
        }
    }

    public enum Command {

        GET_CONFIG("Cmd_GetConfig"),
        RESPONSE("Response");

        private String command;

        Command(String name) {
            command = name;
        }

        @Override
        public String toString() {
            return command;
        }
    }

    public enum Action {
        GET_CONFIG("Cmd_GetConfig"),
        REQUEST("Request");

        private String action;

        Action(String action) {
            this.action = action;
        }

        @Override
        public String toString() {
            return action;
        }
    }
}
